/**
 * Created by Administrator on 2016/2/29.
 *gameSence中发送的聊天信息
 */
gpNamespace.LtLayer = cc.Layer.extend({
		sprite1: null,
		sprite2: null,
		tv1: null,
		tv1Touchable: false,
		tv2: null,
		tv2Touchable: false,

		onEnter: function () {
			this._super();
			cc.log("进入ltLayer============================");

			//加载资源
			cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkliaotianRes.liaotian_plist);
			cc.spriteFrameCache.addSpriteFrames(gpNamespace.gameDialogRes.game_dialog_plist);
			//背景
			var background = new cc.Sprite(gpNamespace.liaotian.lt_bg);
			background.attr({
				x: cc.winSize.width / 2,
				y: cc.winSize.height / 2,
				anchorX: 0.5,
				anchorY: 0.5
			});
			this.addChild(background);
			//背景1
			var background1 = new cc.Sprite(gpNamespace.liaotian.lt_bg_hengxian);
			background1.attr({
				x: 58 - 21,
				y: 25,
				anchorX: 0,
				anchorY: 0
			});
			background.addChild(background1);

			//短语
			var sprite1 = new cc.Sprite(gpNamespace.liaotian.lt_top_cyy);
			sprite1.attr({
				x: 0,
				y: 243 + 60,
				anchorX: 0,
				anchorY: 0
			});
			background1.addChild(sprite1);
			this.sprite1 = sprite1;

			//VIP表情
			var sprite2 = new cc.Sprite(gpNamespace.liaotian.lt_top_bq);
			sprite2.attr({
				x: 273,
				y: 243 + 60,
				anchorX: 0,
				anchorY: 0
			});
			background1.addChild(sprite2);
			sprite2.setVisible(false);
			this.sprite2 = sprite2;

			//短语Menu
			var item1 = new cc.MenuItemImage(gpNamespace.liaotian.lt_top_cyy, gpNamespace.liaotian.lt_top_cyy_selected, this.item1CallbackFunction, this);
			item1.attr({
				x: 274 / 2,
				y: 67 / 2
			});
			var menu1 = new cc.Menu(item1);
			menu1.attr({
				x: 0,
				y: 243 + 60
			});
			background1.addChild(menu1);

			//VIP表情Menu
			if(gpNamespace.LoginInfo.VIP){
				var item2 = new cc.MenuItemImage(gpNamespace.liaotian.lt_top_bq, gpNamespace.liaotian.lt_top_bq_selected, this.item2CallbackFunction, this);
			}else{
				var item2 = new cc.MenuItemImage(gpNamespace.liaotian.lt_top_bq_selected, gpNamespace.liaotian.lt_top_bq_selected, this.item2CallbackFunction, this);
			}
			
			item2.attr({
				x: 274 / 2,
				y: 67 / 2
			});
			var menu2 = new cc.Menu(item2);
			menu2.attr({
				x: 273,
				y: 243 + 60
			});
		
			background1.addChild(menu2);

			//滚轮控件
			var tv1 = new tableViewPro.TableView(this, cc.size(496, 290), 0);
			tv1.setPosition(22, 12);
			tv1.setAnchorPoint(0, 0);
			background1.addChild(tv1);
			this.tv1 = tv1;

			this.tv1.clearData();
			this.tv1.addData({
				"barInfo": {"width": 4, "height": 0},
				"sameItem": [
				             {
			            	 		 "dataType": 1,
				            	 	 "headInfo": {
				            		 "headType": 0,
				            		 "offset": 0,
				            		 "imageName": "",
				            		 "width": 0
				            	 },
				            	 "pageInfo": {"colCount": 1},
				            	 "cellInfo": {
				            		 "cellType": 0,
				            		 "cellCount": 7,
				            		 "imageName": "#pdk_lt_012u.png",
				            		 "height": 50
				            	 }
				             }
				             ]
			});
			this.tv1.showData();
			this.tv1Touchable = true;
			this.tv1.setTouchEnabled(true);
			this.tv1.setVisible(true);
			

			//cells上加表现
			var cellCount = this.tv1.currentItem.cellInfo.cellCount;
			var cellType = this.tv1.currentItem.cellInfo.cellType;
			var currentPage = this.tv1.currentPage;
			for (var a = 0; a < cellCount; a++) {
				var cell;
				switch (cellType) {
				case 0://sprite
				cell = currentPage.getChildByTag(a);
				break;
				case 1://menu
					cell = currentPage.getChildByName(spriteName).getChildByTag(a);
					break;
				}
				this.setTV1Cell(a, cell);
			}

			var tv2 = new tableViewPro.TableView(this, cc.size(496, 270), 0);
			tv2.setPosition(cc.winSize.width/2, cc.winSize.height/2 - 20);
			tv2.setAnchorPoint(0.5, 0.5);
			this.addChild(tv2);  //background1
			this.tv2 = tv2;
			tv2.setVisible(false);

			this.tv2.clearData();
			this.tv2.addData({
				"barInfo": {"width": 10, "height": 40},
				"sameItem": [
				             {
				            	 "dataType": 1,
				            	 "headInfo": {
				            		 "headType": 0,
				            		 "offset": 0,
				            		 "imageName": "",
				            		 "width": 0
				            	 },
				            	 "pageInfo": {"colCount": 4},
				            	 "cellInfo": {
				            		 "cellType": 0,
				            		 "cellCount": 6,
				            		 "imageName": "",
				            		 "height": 150
				            	 }
				             }
				             ]
			});
			this.tv2.showData();
			this.tv2.setTouchEnabled(false);
			this.tv2Touchable = false;

			//cells上加表现
			var cellCount = this.tv2.currentItem.cellInfo.cellCount;
			var cellType = this.tv2.currentItem.cellInfo.cellType;
			var currentPage = this.tv2.currentPage;
			for (var a = 0; a < cellCount; a++) {
				var cell;
				switch (cellType) {
				case 0://sprite
				cell = currentPage.getChildByTag(a);
				break;
				case 1://menu
					cell = currentPage.getChildByName(spriteName).getChildByTag(a);
					break;
				}
				this.setTV2Cell(a, cell);
			}

		},
//
		item1CallbackFunction: function (sender) {
			cc.log(" lt item1CallbackFunction ");
			
//			xzgbNamespace.setMenuEnable(sender);

//			xzgbNamespace.SoundManager.playEffect(xzgbNamespace.soundRes.ButtonEffect, false);

			this.sprite1.setVisible(true);
			this.sprite2.setVisible(false);

			this.tv1Touchable = true;
			this.tv1.setTouchEnabled(true);

			this.tv2Touchable = false;
			this.tv2.setTouchEnabled(false);

			this.tv1.setVisible(true);
			this.tv2.setVisible(false);
		},
//
		item2CallbackFunction: function (sender) {
			cc.log(" lt item2CallbackFunction ");
			cc.log(">>>>>>>>>>>>>>>>>>>>>>>>>>"+gpNamespace.LoginInfo.VIP);
			if (!gpNamespace.LoginInfo.VIP)
				return;

//			xzgbNamespace.SoundManager.playEffect(xzgbNamespace.soundRes.ButtonEffect, false);
			cc.log("我是VIP========================");
			this.sprite1.setVisible(false);
			this.sprite2.setVisible(true);

			this.tv1Touchable = false;
			this.tv1.setTouchEnabled(false);

			this.tv2Touchable = true;
			this.tv2.setTouchEnabled(true);

			this.tv1.setVisible(false);
			this.tv2.setVisible(true);
		},
//
		setTV1Cell: function (index, cell) {
			var str = index + 13;
			if (str < 10)
				str = "0" + str;
			var spriteName = "#pdk_lt_0" + str + "u.png";
			
			cc.log("setTV1Cell spriteName:" + spriteName);
			
//			var sprite = new cc.Sprite(spriteName);
//			sprite.attr({
//				x: cell.getContentSize().width / 2,
//				y: cell.getContentSize().height / 2
//			});
//			cell.addChild(sprite);

			var item = new cc.MenuItemImage(spriteName, spriteName, this.greenCallbackFunction, this);
			item.attr({
				x: item.getContentSize().width / 2,
				y: item.getContentSize().height / 2
			});
			item.setUserData(index);
			
			var menu = new tableViewPro.Menu(item);
			menu.attr({
				x:0,
				y:5
			});
			cell.addChild(menu);
			menu.setTouchListener();
			this.scheduleOnce(function () {
				menu.setTouchArea(this.tv1.clippingNode.getContentSize(), this.tv1.clippingNode.getParent().convertToWorldSpace(this.tv1.clippingNode.getPosition()));
			}, 0.5);
		},

		setTV2Cell: function (index, cell) {
			var str = index + 5;
			if(str<10){
				str = "0"+str;
			}
			var spriteName = "#pdk_lt_0" + str + "u.png";
			var sprite = new cc.Sprite(spriteName);
			sprite.attr({
				x: cell.getContentSize().width / 2,
				y: cell.getContentSize().height / 2
			});
			cell.addChild(sprite, 2);

			var item = new cc.MenuItemImage(spriteName, spriteName, this.green1CallbackFunction, this);
			item.attr({
				x: item.getContentSize().width / 2 ,
				y: item.getContentSize().height / 2 + 10
			});
			item.setUserData(index);
			var menu = new tableViewPro.Menu(item);
			menu.attr({
				x:0,
				y:0
			});
			cell.addChild(menu);
			menu.setTouchListener();
			this.scheduleOnce(function () {
				menu.setTouchArea(this.tv2.clippingNode.getContentSize(), this.tv2.clippingNode.getParent().convertToWorldSpace(this.tv2.clippingNode.getPosition()));
			}, 0.5);
		},

		greenCallbackFunction: function (sender) {
			cc.log("发送短语");

//			xzgbNamespace.setMenuEnable(sender);

//			xzgbNamespace.SoundManager.playEffect(xzgbNamespace.soundRes.ButtonEffect,false);

			var index = sender.getUserData();

			this.getParent().getParent().removeFromParent(true);

			xinyou.netManager.userTalk(index + 1);  //ht
		},

		green1CallbackFunction: function (sender) {
			cc.log("发送表情");

//			xzgbNamespace.setMenuEnable(sender);

//			xzgbNamespace.SoundManager.playEffect(xzgbNamespace.soundRes.ButtonEffect,false);

			var index = sender.getUserData();

			this.getParent().getParent().removeFromParent(true);

			xinyou.netManager.userTalk(index + 51);
		}
});

